package com.njlime.cms.job;

import com.njlime.cms.entities.Community;
import com.njlime.cms.entities.Information;
import com.njlime.cms.service.sys.CommunityService;
import com.njlime.cms.service.sys.InformationService;
import com.njlime.core.helper.GlobalValue;
import com.njlime.core.helper.PageBean;
import com.njlime.core.utils.DateUtil;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;


/**
 * Created by Dean on 2017/10/12.
 */
@Service
public class RegularPublishJob {

    Logger logger = Logger.getLogger(RegularPublishJob.class);
    @Resource
    private InformationService informationService;
    @Resource
    private CommunityService communityService;

    /**
     * 更新发布状态
     */
    public void publish(){
        PageBean pageBean = new PageBean();
        pageBean.setPageSize(Integer.MAX_VALUE);
        Date publish_time = DateUtil.getDayStart(new Date());
       //定时发布资讯
        Information info = new Information();
        info.setPublish_status(GlobalValue.PublishStatus.UNPUBLISHED);
        info.setPublish_time(publish_time);
        pageBean = informationService.list(pageBean, info, true);
        if(pageBean.getMapList()!=null && pageBean.getMapList().size() > 0){
            for(int i = 0;i<pageBean.getMapList().size(); i++){
                Integer id = (Integer)pageBean.getMapList().get(i).get("id");
                Information information = informationService.findOne(id);
                information.setPublish_status(GlobalValue.PublishStatus.PUBLISHED);
                informationService.editInfo(information);
            }
        }
       //定时发布社区
        pageBean.setMapList(null);
        Community com = new Community();
        com.setPublish_status(GlobalValue.PublishStatus.UNPUBLISHED);
        com.setPublish_time(publish_time);
        pageBean = communityService.list(pageBean, com, true);
        if(pageBean.getMapList()!=null && pageBean.getMapList().size() > 0){
            for(int i = 0;i<pageBean.getMapList().size(); i++){
                Integer id = (Integer)pageBean.getMapList().get(i).get("id");
                Community tc = communityService.findOne(id);
                tc.setPublish_status(GlobalValue.PublishStatus.PUBLISHED);
                communityService.update(tc);
            }
        }
    }

}
